<?php 
/*
**********************************************
JoomLeague RANKING MODULE
Copyright (c) 2007 JoomLeague.de
**********************************************
This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* RANKING MODULE
*
* 2007-08-31 - Version 0.91
*
* Displays the the actual ranking of the given project.
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JoomLeague at:
http://www.joomleague.de//
**********************************************
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

if (file_exists($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php')) {
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php');
} 
else{include_once($mosConfig_absolute_path.'/components/com_joomleague/language/english.php');
}

require_once ($mosConfig_absolute_path.'/administrator/components/com_joomleague/joomleague.class.php');
require_once ($mosConfig_absolute_path.'/components/com_joomleague/js/joomleague_tabs.js.php');?>

<!-- To remain XHTML compatibility move this stylesheet link to the head of your index.php -->
<link rel="stylesheet" type="text/css" href="<?php echo $mosConfig_live_site;?>/components/com_joomleague/css/joomleague_tabs.css" />
<!-- Stylesheet link END -->

<script language="javascript" type="text/javascript">
<!-- // --><![CDATA[ 
var dhtml_ranking<?php echo $module->id; ?> = new mosDHTML();
// ]]></script>

<?php
#comment this line for mambo version < 4.5.1
$params = mosParseParams( $module->params );
$p = $params->project; //default 0
$t_count = intval($params->showteams); //default "all" => 0
$project = new JoomLeague( $database );
$project->load($p);
$table_config=$project->getTemplateConfig("show_table.tpl");
$colors = $project->getColors($table_config['colors']);
$joomleague_ranking = & new Ranking();
$joomleague_ranking->setProjectConfig($project, $table_config['rankingorder']);
$counttabs=0;
$temprank='';
if($params->show_multiple_rankings == 0) {
  $divtext[$counttabs]='';
  $starttabs[$counttabs]=0;
  $tabsmod[$counttabs]=0;
} else {
  if($params->show_multiple_rankings == 1 && $params->show_ranking_home == 1){
    $divtext[$counttabs]='<div id="page_ranking_home'.$module->id.'" class="pagetext">';
    $tabsmod[$counttabs]=1;
    $counttabs++;
  }
  if($params->show_multiple_rankings == 1 && $params->show_ranking_all == 1){
    $divtext[$counttabs]='<div id="page_ranking_all'.$module->id.'" class="pagetext">';
    $tabsmod[$counttabs]=0;
    $counttabs++;
  }
  if($params->show_multiple_rankings == 1 && $params->show_ranking_away == 1){
    $divtext[$counttabs]='<div id="page_ranking_away'.$module->id.'" class="pagetext">';
    $tabsmod[$counttabs]=2;
    $counttabs++;
  }
  $counttabs--;
}

?>

<?php if($params->show > 0){?><div align="center"><b><?php echo $project->name;?></b></div><?php }?>

<?php
  if($params->show_multiple_rankings == 1){echo '<table cellspacing="0" cellpadding="1" border="0" width="100%"><tr align="center">';
  if($params->show_ranking_home == 1){echo '<td width="33%" id="tab_ranking_home'.$module->id.'" class="offtab" onclick="dhtml_ranking'.$module->id.'.cycleTab(this.id)">'._JL_MODULE_RANKING_HOME.'</td>';}
  if($params->show_ranking_all == 1){echo '<td width="34%" id="tab_ranking_all'.$module->id.'" class="offtab" onclick="dhtml_ranking'.$module->id.'.cycleTab(this.id)">'._JL_MODULE_RANKING_OVERALL.'</td>';}       
  if($params->show_ranking_away == 1){echo '<td width="33%" id="tab_ranking_away'.$module->id.'" class="offtab" onclick="dhtml_ranking'.$module->id.'.cycleTab(this.id)">'._JL_MODULE_RANKING_AWAY.'</td>';}
echo '</tr></table>';}?>


<!-- Normal Ranking without tabs START -->
<?php
for($i=0;$i<=$counttabs;$i++) {
$anzahlcol = 1;
if ($divtext[$i]<>'') echo $divtext[$i];
echo '<table width="100%" align="center" border="0" cellpadding="2" cellspacing="0">';
echo '<tr class="sectiontableheader">';
echo '<td align="center">&nbsp;</td>';
echo '<td align="center">&nbsp;</td>';
  if($params->show_logo_small_table){echo '<td align="center">&nbsp;</td>';$anzahlcol++;}
  if($params->show_cnt_matches){echo '<td align="center"><b>S</b></td>';$anzahlcol++;}
  if($params->show_won){echo '<td align="center"><b>'._JL_TABLE_WON.'</b></td>';$anzahlcol++;}
  if($params->show_draw){echo '<td align="center"><b>'._JL_TABLE_DRAW.'</b></td>';$anzahlcol++;}
  if($params->show_loss){echo '<td align="center"><b>'._JL_TABLE_LOSS.'</b></td>';$anzahlcol++;}
  if($params->show_results){echo '<td align="center"><b>T</b></td>';$anzahlcol++;}
  if($params->show_diff){echo '<td align="center"><b>D</b></td>';$anzahlcol++;}
  if($params->show_points){echo '<td align="center"><b>P</b></td>';$anzahlcol++;}
echo '</tr>';
  $table = $joomleague_ranking->getRanking($tabsmod[$i],$project->current_round, 1, $table_config['show_negative_points'], $table_config['sort_table'], $params->division);
  $counter=1; $k = 0; $j = 0; $anzahl_favs=0;
// use fav_team from cookie if exist
  if (isset($_COOKIE['joomleague_user_fav_team_p_'.$project->id.''])) $favteam=explode(",", $_COOKIE['joomleague_user_fav_team_p_'.$project->id.'']);
// use fav_team from tip_members table if no cookie exist and user is logged in
  if (!isset($_COOKIE['joomleague_user_fav_team_p_'.$project->id.'']) && $my->id > 0) {
  $query = sprintf("SELECT fav_team FROM #__joomleague_tip_members WHERE user_id = %s AND project_id = %s", $my->id, $project->id);
  $project->_db->setQuery($query);
  $user_db_fav_team = $project->_db->loadResult();
  $query = sprintf("SELECT team_id FROM #__joomleague_team_joomleague WHERE id = %s AND project_id = %s", $user_db_fav_team, $project->id);
  $project->_db->setQuery($query);
  $user_db_favteam = $project->_db->loadResult();
  $favteam=explode(",", $user_db_favteam);
  }
// use fav_team from project configuration if no cookie exist and user is not logged in  
  if (!isset($_COOKIE['joomleague_user_fav_team_p_'.$project->id.'']) && $my->id == 0) $favteam=explode(",", $project->fav_team);
foreach($table as $item){
  if($params->names == "0") $nametype = $item->team->short_name;
  if($params->names == "1") $nametype = $item->team->middle_name;
  if($params->names == "2") $nametype = $item->team->name;
  $query = sprintf("SELECT club_id FROM #__joomleague_teams WHERE id =%s",$item->team->id);
  $project->_db->setQuery($query);
  $club_id = $project->_db->loadResult();
  $query = sprintf("SELECT logo_small FROM #__joomleague_clubs WHERE id =%s",$club_id);
  $project->_db->setQuery($query);
  $small_club_icon = $project->_db->loadResult();
  $query = sprintf("SELECT website FROM #__joomleague_clubs WHERE id =%s",$club_id);
  $project->_db->setQuery($query);
  $website = $project->_db->loadResult();
  $query = sprintf("SELECT id FROM #__joomleague_team_joomleague WHERE team_id =%s",$item->team->id);
  $project->_db->setQuery($query);
  $team_id = $project->_db->loadResult();              
  if (($t_count) && ($counter > $t_count)) {
    if ($params->show_favs==0) break;
    if ($anzahl_favs>=count($favteam)) {
      if ($counter > $t_count+1) echo '<tr class="sectiontableentry2"><td align="right" bgcolor="">:&nbsp;</td><td colspan="'.$anzahlcol.'" align="right" bgcolor="">&nbsp;</td></tr>';
      break;
    }
  }
  if($params->highlight == 1){$color = "";
   if($counter == $colors[$j]["from"]) $color = $colors[$j]["color"];
   if($counter > $colors[$j]["from"] && $counter <= $colors[$j]["to"]) $color = $colors[$j]["color"];
   if($counter == $colors[$j]["to"]) $j++;
  }  
  if ((($t_count) && ($counter > $t_count) && (in_array($item->team->id, $favteam))) || ($counter<=$t_count) || (!$t_count)) {
  $class = ($k == 0)? $params->sectiontableentry1 : $params->sectiontableentry2;
  $format = "%s";
  if($params->fav_team_bold == 1 && in_array($item->team->id, $favteam)) {$format = "<b>%s</b>";}
  if (in_array($item->team->id, $favteam)) $anzahl_favs++;
echo '<tr class="'.$class.'">';
echo '<td bgcolor="'.$color.'" align="right">'; if (($item->rank!=$temprank) || ($counter==1)) printf($format, $item->rank); else echo "&nbsp;"; echo '&nbsp;</td>';
  if ($params->show_logo_small_table){echo '<td valign="top" bgcolor="'.$color.'" align="center">
  <img src="'.$mosConfig_live_site.'/'.$small_club_icon.'" alt="'.$nametype.'" title="'.$nametype.'" align="middle" border="0"/></td>';}
echo '<td bgcolor="'.$color.'" align="left" nowrap="nowrap">';
  if ($params->link_team_to == 0){printf($format, $nametype); echo '</td>';}    
  if ($params->link_team_to == 1){echo '<a href="'.sefRelToAbs('index.php?option=com_joomleague&amp;func=showPlayers&amp;p='.$project->id.'&amp;ttid='.$team_id.'&amp;Itemid='.$Itemid).'">';
  printf($format, $nametype); echo '</a></td>';}     
  if ($params->link_team_to == 2){echo '<a href="'.sefRelToAbs('index.php?option=com_joomleague&amp;func=showClubInfo&amp;p='.$project->id.'&amp;cid='.$club_id.'&amp;Itemid='.$Itemid).'">';
  printf($format, $nametype); echo '</a></td>';} 
  if ($params->link_team_to == 3 && $website!=""){echo '<a href="'.$website.'" target="_blank" >';
  printf($format, $nametype); echo '</a></td>';}    
  if($params->show_cnt_matches){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, $item->cnt_matches); echo '</td>';}
  if($params->show_won){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, $item->cnt_won); echo '</td>';}
  if($params->show_draw){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, $item->cnt_draw); echo '</td>';}
  if($params->show_loss){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, $item->cnt_lost); echo '</td>';}
  if($params->show_results){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, sprintf("%s:%s",$item->sum_matchpart1_result,$item->sum_matchpart2_result)); echo '</td>';}
  if($params->show_diff){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, $item->diff_matchpart_results); echo '</td>';}
  if($params->show_points){echo '<td bgcolor="'.$color.'" align="center">'; printf($format, $item->sum_points); if ($table_config['show_negative_points']==1) {echo ':'; printf($format, $item->neg_points);} echo '</td>';}
echo '</tr>';
  $anzeige=0;}else{if (($anzeige==0) && ($project->fav_team<>'')) echo '<tr class="sectiontableentry2"><td align="right" bgcolor="">:&nbsp;</td><td colspan="'.$anzahlcol.'" align="right" bgcolor="">&nbsp;</td></tr>';$anzeige=1;}
  $k = 1 - $k; $counter++;$temprank=$item->rank;}
echo '</table>';
  if($t_count){echo '<table width="100%"><tr><td align="center">';
              echo '<a href="'.sefRelToAbs('index.php?option=com_joomleague&amp;func=showRanking&amp;p='.$project->id.'&amp;Itemid='.$Itemid).'">';
              echo _JL_MODULE_RANKING_TOTAL.'</a></td></tr></table>';}
if ($divtext[$i]<>'') echo '</div>';}

if ($params->show_multiple_rankings == 1) {
  echo '<script language="javascript" type="text/javascript">';
  if ($params->show_ranking_home == 1) echo "dhtml_ranking".$module->id.".cycleTab('tab_ranking_home".$module->id."');";
  if ($params->show_ranking_away == 1) echo "dhtml_ranking".$module->id.".cycleTab('tab_ranking_away".$module->id."');";
  if ($params->show_ranking_all == 1) echo "dhtml_ranking".$module->id.".cycleTab('tab_ranking_all".$module->id."');";
  echo '</script>';
}
?>
